package com.mandala.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mandala.LDAPConnectService;
import com.mandala.entry.Org;
import com.mandala.entry.ResponseWrapper;
import com.mandala.service.JsonResult;
import com.mandala.service.OdmUserRepo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/org")
@Api("组织机构查询接口")
public class OrgController {
	@Autowired
	private OdmUserRepo odmUserRepo;
	@Autowired
	private JsonResult jsonResult;
	@Autowired
	private LDAPConnectService connectService;
	
	static Map<Long, Org> orgs = Collections.synchronizedMap(new HashMap<Long, Org>());
	
    @ApiOperation(value = "根据组织机构ID", notes = "组织机构查询")
    @ApiImplicitParam(name = "ou", value = "机构ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getOrgByOu/{ou}" , method = RequestMethod.GET)
    public  ResponseWrapper getOrgByOu(@PathVariable("ou") String ou) {
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	Org org = new Org();
    	org = odmUserRepo.findOrgByOu(ou);
    	ResponseWrapper response = new ResponseWrapper();
		if(org != null){
			response = jsonResult.markSuccess(org);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    @ApiOperation(value="获取所有机构列表", notes="所有机构查询")
    @RequestMapping(value={""}, method=RequestMethod.GET)
    public ResponseWrapper getOrgList() {
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	ResponseWrapper response = new ResponseWrapper();
    	List<Org> list = odmUserRepo.findAllOrg();
    	if(list.size()>0){
			response = jsonResult.markSuccess(list);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
}